<!-- 用户信息 -->
<template>
  <div class="box_list">
    <div class="box_xi">修改密码</div>
    <div class="box_from">
      <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="135px">
        <el-form-item label="旧密码" prop="oldPassword ">
          <el-input v-model="ruleForm.oldPassword "></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="newPassword">
          <el-input type="password" v-model="ruleForm.newPassword"></el-input>
        </el-form-item>
        <el-form-item label="新密码确认" prop="passWord">
          <el-input type="password" v-model="ruleForm.passWord" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm(ruleForm)">提交</el-button>
          <el-button @click="resetForm()">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
import Apiadmin from "@/api/admin"
import admin from '@/store/admin';

export default {
  data() {
    var checkAge = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入旧密码'));
      }
    };
    var validatePass = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入新密码'));
      }
    };
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'));
      } else if (value !== this.ruleForm.newPassword) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        oldPassword: '',
        newPassword: '',
        passWord: '',
      },
      rules: {
        oldPassword: [{
          validator: checkAge,
          trigger: 'blur'
        }],
        newPassword: [{
          validator: validatePass,
          trigger: 'blur'
        }],
        passWord: [{
          validator: validatePass2,
          trigger: 'blur'
        }]
      }
    };
  },
  methods: {
    //重置
    resetForm(formName) {
      this.ruleForm = {
        oldPassword: '',
        newPassword: '',
        passWord: '',
        _gp: 'admin',
        _mt: 'newPassword'
      }
    },
    submitForm() {
      console.log(Apiadmin.passNew)
      const data = {
        oldPassword: this.ruleForm.oldPassword,
        newPassword: this.ruleForm.newPassword,
      }
      Apiadmin.passNew(data).then(res => {
        console.log(res)
        let { data, errno, errmsg } = res
        if (errno == 200) {
          this.$message.success('修改成功')
          this.$router.replace('/index')
          this.$nextTick(() => {
            this.$router.push({ path: "/index/redirect/" + routes.path });
          });
        } else {
          this.$message.error('修改失败')
        }
      })
    }
  }
}
</script>
<style scoped lang="less">
.box_list {
  width: 600px;
  height: 300px;
  border: 1px solid #cccccc;
  margin: auto;

  .box_xi {
    text-align: left;
    text-indent: 15px;
    border-bottom: 1px solid #cccccc;
    line-height: 40px;
    font-weight: bold;
  }
}

.box_from {
  margin: 20px;
}
</style>